package com.common.zrd.mapper.mymapper;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;

/**
 * @author: zrd
 * @Date: 2020/4/15 14:39
 * @Description:
 */
@Slf4j
public class DisableProvider extends MapperTemplate {
    public DisableProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String disable(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append(" set ");
        sql.append("del_flag").append(" = ").append("1");
        sql.append(SqlHelper.wherePKColumns(entityClass, true));
        return sql.toString();
    }

    public String enable(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append(" set ");
        sql.append("del_flag").append(" = ").append("0");
        sql.append(SqlHelper.wherePKColumns(entityClass, true));
        return sql.toString();
    }
    /**
     * 通过主键批量更新不为null的字段
     *
     * @param ms
     * @return String [返回类型说明]
     * @see [类、类#方法、类#成员]
     */
    public String updateList(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));

        // 获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        // 获取主键列
        Set<EntityColumn> pkList = EntityHelper.getPKColumns(entityClass);
        sql.append("<trim prefix=\"SET\" suffixOverrides=\",\">");
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isUpdatable()) {
                if (("lastmodifytime".equalsIgnoreCase(column.getColumn())
                        || "updateTime".equalsIgnoreCase(column.getColumn())) && null != column.getJavaType()
                        && Date.class.isAssignableFrom(column.getJavaType())) {
                    // 最后更新时间使用数据库当前时间now()填充
                    sql.append(column.getColumn() + " = now()" + ",");
                } else {
                    sql.append("<trim prefix=\" " + column.getColumn() + " = CASE\" suffix=\" END,\">");
                    sql.append("<foreach collection=\"list\" item=\"updateItem\">");
                    if (column.getJavaType() != String.class) {
                        sql.append(" <if test=\"updateItem." + column.getProperty() + " != null  \" >");
                    } else {
                        sql.append("<if test=\" updateItem." + column.getProperty() + " != null  " +
                                "and updateItem." + column.getProperty() + "!= ''\">");

                    }
                    sql.append("<trim prefix=\" WHEN \" suffix=\" THEN \" suffixOverrides=\" AND \">");
                    for (EntityColumn pk : pkList) {
                        sql.append(pk.getColumnEqualsHolder("updateItem") + " AND ");
                    }
                    sql.append("</trim>");
                    sql.append("COALESCE(" + column.getColumnHolder("updateItem") + "," + column.getColumn() + ")");
                    // 参数值不为空则进行修改
                    // SqlHelper.getIfNotNull("updateItem", column, column.getColumnHolder("updateItem"), isNotEmpty());
                    // 参数值为空则赋予字段原来的值
                    // SqlHelper.getIfIsNull("updateItem", column, column.getColumn(), isNotEmpty());
                    sql.append("</if>");
                    sql.append("</foreach>");
                    sql.append("</trim>");
                }
            }
        }
        sql.append("</trim>");

        // where条件
        sql.append("<where>");
        sql.append(
                "<foreach collection=\"list\" item=\"updateItem\" open=\"(\" close=\")\" separator=\"OR\">");
        sql.append("<trim prefix=\"\" suffix=\"\" suffixOverrides=\" AND \">");
        for (EntityColumn pk : pkList) {
            sql.append(pk.getColumnEqualsHolder("updateItem") + " AND ");
        }
        sql.append("</trim>");
        sql.append("</foreach>");
        sql.append("</where>");
        log.info("批量更新" + sql.toString());
        return sql.toString();
    }

    public String updateList111(MappedStatement ms) {
        final Class entityClass = getEntityClass(ms);
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        // 获取主键
        Set pkColumnSet = EntityHelper.getPKColumns(entityClass);
        EntityColumn entityColumn = (EntityColumn) Lists.newArrayList(pkColumnSet).get(0);
        String pkColumn = entityColumn.getColumn();
        String pkProperty = entityColumn.getProperty();

        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isUpdatable()) {
                sql.append("  ");
                sql.append("    ");
                sql.append("      ");
                sql.append("         when ").append(pkColumn).append("=#{i.").append(pkProperty).append("} then #{i." + column.getEntityField().getName() + "}");
                sql.append("      ");
                sql.append("    ");
                sql.append("  ");
            }
        }
        sql.append("WHERE ");
        sql.append(pkColumn).append(" IN ");
        sql.append("#{i.").append(pkProperty).append("}");

        return sql.toString();
    }

    public String updateList1(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\";\">");
        sql.append(SqlHelper.updateTable(entityClass, this.tableName(entityClass)));
        sql.append(SqlHelper.updateSetColumns(entityClass, "item", true, this.isCheckExampleEntityClass()));

        Set<EntityColumn> columnSet = EntityHelper.getPKColumns(entityClass);
        Iterator var4 = columnSet.iterator();
        sql.append("<where>");
        while (var4.hasNext()) {
            EntityColumn column = (EntityColumn) var4.next();
            sql.append(" AND " + column.getColumn() + " = " + column.getColumnHolder("item"));
        }
        sql.append("</where>");
        sql.append("</foreach>");
        log.info(sql.toString());
        return sql.toString();
    }
}
